/*
 * ImportRespondentsListJDialog.java
 *
 * Created on 10.05.2009, 2:14:50
 */
package morenoapp.components.input;

import java.awt.event.KeyEvent;
import javax.swing.ActionMap;
import morenoapp.MorenoApp;
import morenoapp.model.data.MorenoModelBatchJob;
import morenoapp.model.data.MorenoModelData;
import morenoapp.model.data.MorenoModelFace;
import morenoapp.model.data.MorenoRespondentFace;
import org.jdesktop.application.Action;

/**
 * 
 * @author dmitry.mamonov
 */
public class ImportRespondentsListJDialog extends javax.swing.JDialog {
    /** Creates new form ImportRespondentsListJDialog */
    public ImportRespondentsListJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        ActionMap aMap = MorenoApp.getInstance().getContext().getActionMap(this);
        jButtonCancel.setAction(aMap.get("actionCancel"));
        jButtonDoImport.setAction(aMap.get("actionDoImport"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPaneRespondentsList = new javax.swing.JScrollPane();
        jTextAreaRespondentsList = new javax.swing.JTextArea();
        jButtonDoImport = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPaneRespondentsList.setName("jScrollPaneRespondentsList"); // NOI18N

        jTextAreaRespondentsList.setColumns(20);
        jTextAreaRespondentsList.setFont(new java.awt.Font("Tahoma", 0, 11));
        jTextAreaRespondentsList.setRows(5);
        jTextAreaRespondentsList.setName("jTextAreaRespondentsList"); // NOI18N
        jTextAreaRespondentsList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextAreaRespondentsListKeyPressed(evt);
            }
        });
        jScrollPaneRespondentsList.setViewportView(jTextAreaRespondentsList);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(ImportRespondentsListJDialog.class);
        jButtonDoImport.setText(resourceMap.getString("jButtonDoImport.text")); // NOI18N
        jButtonDoImport.setName("jButtonDoImport"); // NOI18N

        jButtonCancel.setText(resourceMap.getString("jButtonCancel.text")); // NOI18N
        jButtonCancel.setName("jButtonCancel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneRespondentsList, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(111, Short.MAX_VALUE)
                .addComponent(jButtonDoImport)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonCancel)
                .addGap(5, 5, 5))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPaneRespondentsList, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCancel)
                    .addComponent(jButtonDoImport)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextAreaRespondentsListKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextAreaRespondentsListKeyPressed
        if (evt.getKeyCode()==KeyEvent.VK_ENTER && evt.isControlDown()){
            actionDoImport();
        }
    }//GEN-LAST:event_jTextAreaRespondentsListKeyPressed

    @Action
    public void actionDoImport() {
        final MorenoModelFace model = MorenoApp.getModel();
        model.doBatchJob(new MorenoModelBatchJob() {
            @Override
            public void processModel(MorenoModelData model) {
                for (String name : jTextAreaRespondentsList.getText().split("\\n")) { //NOI18N
                    name = name.trim();
                    if (name.isEmpty() == false) {
                        MorenoRespondentFace resp = null;
                        for (MorenoRespondentFace r : model.getRespondentsList()) {
                            if (r.getName().isEmpty()) {
                                resp = r;
                                break;
                            }
                        }
                        if (resp == null) {
                            resp = model.createRespondent();
                        }
                        resp.setName(name);
                    }
                }
            }
        });
        setVisible(false);
    }

    @Action
    public void actionCancel() {
        setVisible(false);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonDoImport;
    private javax.swing.JScrollPane jScrollPaneRespondentsList;
    private javax.swing.JTextArea jTextAreaRespondentsList;
    // End of variables declaration//GEN-END:variables

}
